// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
    let message = cloud.getWXContext()
    try {
        let num = await db.collection('gods').where({
            _openid: message.OPENID,
        }).count()
        if (num.total == 0) {
            return await db.collection('gods').add({
                // data 字段表示需新增的 JSON 数据
                data: {
                    _openid: message.OPENID,
                    name: event.name,
                    avatarUrl: event.avatarUrl,
                    chat_list: [],
                    collect_list: [],
                    gwCar_list: [],
                    btList: [],
                    publish: {
                        card: [],
                        usedGoods: [],
                        lostGoods: []
                    }
                }
            })
        } else {
            return "have"
        }
    } catch (error) {
        console.error(error);
    }

}